//
//  HCNewsListViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/5/25.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCNewsListViewController.h"

#import "HCNewsListModel.h"

#import "NewsListCell.h"

#import "NewsCenterHeadView.h"

@interface HCNewsListViewController () <UITableViewDelegate,UITableViewDataSource> {
    UITableView *tableView_;
    
    HCNewsListModel *listModel;
    
    NewsCenterHeadView *headerView__;
}

@end

@implementation HCNewsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textTitle = @"新闻中心";
    self.dataRequest.delegate = self;
    
    tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, self.centerView.frame.size.height)];
    tableView_.delegate = self;
    tableView_.dataSource = self;
    tableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    headerView__ = [[NewsCenterHeadView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, 200)];
    tableView_.tableHeaderView = headerView__;
    
    tableView_.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"刷新事件");
        [self.dataRequest post_HCNewsList];
    }];
    
    [self.centerView addSubview:tableView_];
    
    [tableView_ registerNib:[UINib nibWithNibName:@"NewsListCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"newslist"];
    
    // Do any additional setup after loading the view.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (listModel) {
        return listModel.news_list.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newslist" forIndexPath:indexPath];
    if (listModel) {
        HCNewsListData *data = listModel.news_list[indexPath.row];
        cell.model = data;
    }
    if (indexPath.row >2) {
        cell.hotView.hidden = YES;
    }else{
        cell.hotView.hidden = NO;;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    HCNewsListData *data = listModel.news_list[indexPath.row];
//    CGFloat hh = [self getSpaceLabelHeight:data.title withWidh:(YBKAppFrame.size.width - 20)];
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HCNewsListData *data = listModel.news_list[indexPath.row];
    [self pushChildrenViewController:@"HCNeswDetailViewController" parameterObject:data.url dataObject:nil];
}

-(CGFloat)getSpaceLabelHeight:(NSString *)str withWidh:(CGFloat)width
{
    
    NSMutableParagraphStyle *paragphStyle=[[NSMutableParagraphStyle alloc]init];
    
    paragphStyle.lineSpacing=0;//设置行距为0
    paragphStyle.firstLineHeadIndent=0.0;
    paragphStyle.hyphenationFactor=0.0;
    paragphStyle.paragraphSpacingBefore=0.0;
    
    NSDictionary *dic=@{
                        
                        NSFontAttributeName:[UIFont systemFontOfSize:15], NSParagraphStyleAttributeName:paragphStyle, NSKernAttributeName:@1.0f
                        
                        };
    CGSize size=[str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return size.height;
    
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [tableView_.mj_header beginRefreshing];
}

-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    NSLog(@"新闻列表成功:%@",dictionary);
    
    listModel = [[HCNewsListModel alloc] initWithDictionary:dictionary error:nil];
    [tableView_.mj_header endRefreshing];
    
    headerView__.imageUrl = [NSString stringWithFormat:@"%@",listModel.img];
    headerView__.topLabelName = [NSString stringWithFormat:@"%@",dictionary[@"news_list"][0][@"title"]];
    
    [tableView_ reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"新闻列表失败;");
    [tableView_.mj_header endRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
